<?php
require_once("includes/pagetop.php");

if(!isset($_SESSION["Allyfolio_User_ID"]) || $_SESSION["Allyfolio_User_ID"]=="")
{
	redirect("index.php");	
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>AllyFolio | Create Page </title>
<link href="css/style.css" rel="stylesheet" type="text/css" media="all"  />
<script src="js/general.js"></script>
<script src="js/functions.js"></script>

<link rel="stylesheet" href="alertify/themes/alertify.core.css" />
<link rel="stylesheet" href="alertify/themes/alertify.default.css" id="toggleCSS" />
<script src="alertify/lib/alertify.min.js"></script>	
<script src="js/jquery1.10.2.min.js"></script>

<script src="js/jquery.placeholder.js"></script>
<script type="text/javascript">
    $(function() { $('input, textarea').placeholder();});
</script>
<style type="text/css">
    .placeholder {
      color: #cccccc;
      }
</style>
<script type="text/javascript" language="javascript">// <![CDATA[
function showHide() {
    var ele = document.getElementById("showHideDiv");
    if(ele.style.display == "block") {
            ele.style.display = "none";
      }
    else {
        ele.style.display = "block";
    }
}
// ]]></script>
<script src="js/modernizr.custom.56223.js"></script>
<style type="text/css">
	.logo{ margin-top:10px;}
	.log_button{
	
	height:auto;
	margin:6px 0;
	overflow:hidden;
	float:left;
	margin-top:10px;
	
	}
	#showHideDiv tr{ width:100%}
</style>
</head>

<body>

	<div id="main_wrapper">
<?php
    $pageheading="Create Page";
    require_once("includes/header.php");
    $data=array();
    $data=array();
    if(isset($_POST['data'])&& count($_POST['data'])>0)
	{
		$data= $_POST['data'];
        
        $check_query=$pdodbobj->checkExistance("af_company_page",array("Comp_Page_Name"=>$data['Comp_Page_Name']));

        if($check_query==0)
		{
			$errorFlag=false;
            if(isset($_FILES["Comp_Page_Pic_file"]) && $_FILES["Comp_Page_Pic_file"]["name"])
            {
				$target_path = 'uploads/comp_page_images/';
				
				list($usec, $sec) = explode(" ", microtime());
				$now = ((string)((float)$usec + (float)$sec));
				$ext = explode('.', basename( $_FILES['Comp_Page_Pic_file']['name']));
				
				if(!in_array($ext,array(".jpg",".jpeg",".png",".bmp",".gif")))
				{
					$fileCounter++;
					$filePath=$now . "." . $ext[count($ext)-1];
					$Fullpath = $target_path .$filePath; 
					
					if(move_uploaded_file($_FILES['Comp_Page_Pic_file']['tmp_name'], $Fullpath)) 
					{
						$data["Comp_Page_Pic"]="uploads/comp_page_images/".$filePath;
					} 
					else
					{
						$errorFlag=true;
						$msg_error= "Looks like there is some problem. Please try after some time.";
					}
				}
				else
				{
					$errorFlag=true;
					$msg_error= "Please Upload image only.";
				}
			}
			else
			{
				$data["Comp_Page_Pic"]="";
			}
			
            if(!$errorFlag) //user did not uplaoded page pic
            {
				$insertqry="insert into af_company_page set User_ID='".$_SESSION['Allyfolio_User_ID']."',Comp_Type_ID=:Comp_Type_ID,Category_Type=:Category_Type,Comp_Page_Name=:Comp_Page_Name,Page_Slug=:Page_Slug,Comp_Page_Pic=:Comp_Page_Pic,Comp_Address=:Comp_Address,Comp_City=:Comp_City,Comp_State=:Comp_State,Comp_Zip=:Comp_Zip,Comp_Phone=:Comp_Phone,Create_Date='".time()."'";
            	$pdodbobj->query($insertqry,array(":Comp_Type_ID"=>$data["Comp_Type_ID"],":Category_Type"=>$data["Category_Type"],":Comp_Page_Name"=>$data["Comp_Page_Name"],":Page_Slug"=>create_slug($data["Comp_Page_Name"]),":Comp_Page_Pic"=>$data["Comp_Page_Pic"],":Comp_Address"=>$data["Comp_Address"],":Comp_City"=>$data["Comp_City"],":Comp_State"=>$data["Comp_State"],":Comp_Zip"=>$data["Comp_Zip"],":Comp_Phone"=>$data["Comp_Phone"]));
			}
            
			if($errorFlag)
			{
				//DO nothing
			}
            else if($pdodbobj->affectedrows>0)
			{
				$_SESSION['msg_success']="Company Page has been created successfully.";
				//$msg_error="Company type successfully.";
                redirect('pagelist.php');
			}
			else
			{
				$msg_error="Looks like there is some problem in creating the page. Please try after some time.";
			}
		}
		else
		{
			$msg_error="This page name has been already taken. Please try another.";
		}	
	}   
    
    $qryCompanyType="select * from af_company_types where status=0 ";
    $arrCompanyType=$pdodbobj->fetch_array($pdodbobj->query($qryCompanyType,array()));
?>
    
    
    <!--=================Body Contain Start=======================-->
	<div class="wrapper">
		<div class="header">
        	<div class="contain-wrap2" style="margin-top:30px;">
                <form method="post" enctype="multipart/form-data" >
            	<div class="creat_txt" style="color:#000; font-size:32px;">Create page for</div>
				<!--<div class="company" onclick="return showHide();">Company</div>
	            <div id="showHideDiv" style="display:none">-->
                <div class="company">
                <?php
                    if(count($arrCompanyType)>0)
                    {
                ?>
                    <select name="data[Comp_Type_ID]" id="Comp_Type_ID">
                        <option value="">Company</option>
                    <?php
                        for($i=0;$i<count($arrCompanyType);$i++)
                        {
                            $selected="";
                            if(is_array($data) && isset($data["Comp_Type_ID"]) && $arrCompanyType[$i]['company_type_id']==$data["Comp_Type_ID"])
                            {
                                $selected=" selected ";
                            }
                    ?>
                        <option value="<?php echo $arrCompanyType[$i]['company_type_id']; ?>" <?php echo $selected ?>><?php echo $arrCompanyType[$i]['company_type_name']; ?></option>
                    <?php        
                        }
                    ?>
                    </select>
                <?php        
                    }
                    else
                    {
                        echo "Please contact to Administrator to create the company";
                    }
                    
                ?>
                    
                </div>
		            <table width="336px" cellpadding="0" cellspacing="0" border="0" style="border:1px solid #abe1fa; margin:0 auto; margin-top:10px;" >
						<tr>
							<td><div style="text-align:center; font-size:26px;">Local Business</div></td>
						</tr>
						<tr>
							<td>
								<div style="width:98%; margin:0 auto; padding:5px; padding-bottom:2px; padding-top:2px">
									<input type="text" class="local_box" name="data[Category_Type]" id="Category_Type" placeholder="Type a Category" value="<?php if(is_array($data) && isset($data["Category_Type"]) ){ echo $data["Category_Type"];} ?>" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="width:98%; margin:0 auto; padding:5px; padding-bottom:2px; padding-top:2px">
									<input type="text" class="local_box" name="data[Comp_Page_Name]" id="Comp_Page_Name" placeholder="Name" value="<?php if(is_array($data) && isset($data["Comp_Page_Name"]) ){ echo $data["Comp_Page_Name"];} ?>" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="width:98%; margin:0 auto; padding:5px; padding-bottom:2px; padding-top:2px">
									<input type="text" class="local_box"  name="data[Comp_Address]" id="Comp_Address" placeholder="Street Address" value="<?php if(is_array($data) && isset($data["Comp_Address"]) ){ echo $data["Comp_Address"];} ?>" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="width:98%; margin:0 auto; padding:5px; padding-bottom:2px; padding-top:2px">
									<input type="text" class="local_box"  name="data[Comp_City]" id="Comp_City" placeholder="City" value="<?php if(is_array($data) && isset($data["Comp_City"]) ){ echo $data["Comp_City"];} ?>" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="width:98%; margin:0 auto; padding:5px; padding-bottom:2px; padding-top:2px">
									<input type="text" class="local_box" name="data[Comp_State]" id="Comp_State" placeholder="State" value="<?php if(is_array($data) && isset($data["Comp_State"]) ){ echo $data["Comp_State"];} ?>" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="width:98%; margin:0 auto; padding:5px; padding-bottom:2px; padding-top:2px">
									<input type="text" class="local_box" name="data[Comp_Zip]" id="Comp_Zip" placeholder="Zip Code" value="<?php if(is_array($data) && isset($data["Comp_Zip"]) ){ echo $data["Comp_Zip"];} ?>" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<div style="width:98%; margin:0 auto; padding:5px; padding-bottom:2px">
									<input type="text" class="local_box" name="data[Comp_Phone]" id="Comp_Phone" placeholder="Phone No." value="<?php if(is_array($data) && isset($data["Comp_Phone"]) ){ echo $data["Comp_Phone"];} ?>" />
								</div>
							</td>
						</tr>
						<tr>
							<td>
								<input type="file" id="Comp_Page_Pic_file" name="Comp_Page_Pic_file" accept="image/*"  />
							<?php
								if(is_array($data) && isset($data["Comp_Page_Pic"]) )
								{
							?>	
									<div >
										<img id="Comp_Page_Pic_img" src="<?php if($data["Comp_Page_Pic"]!="" ){ echo "uploads/image_crop.php?image=/".$data["Comp_Page_Pic"]."&width=80&height=80&cropratio=80:80"; } else { echo "uploads/image_crop.php?image=/images/dummy_image.jpg&width=80&height=80&cropratio=80:80"; } ?>" alt="<?php if(is_array($data) && isset($data["Comp_Page_Name"]) ){ echo $data["Comp_Page_Name"];} ?>" />
									</div>
							<?php 
								}
							?>	
							</td>
						</tr>
					</table>
				<!--</div>-->
				<div style="width:336px; margin:10px auto;"><input type="checkbox" id="terms_check" name="terms_check" style="margin-left:10px;" checked="checked" /><label class="agr">I Agree the terms & Conditions</label></div>
				<div style="width:336px; margin:10px auto;"><input type="submit" value="Get Start" class="strat" onclick="return checkValidation();" /></div>
            	<div class="clearfix"></div>
            </div> 
            </form>    
		     <div class="clearfix"></div>
		</div>
 	</div>
            
    <!--=================Body Contain end=======================-->
    </div>
    <?php
    require_once("includes/footer.php");
?>
<script>
function checkValidation()
{
    if(document.getElementById("Comp_Type_ID").value.trim()=="")
	{
		alertify.alert("Please select a Company Type.");
		document.getElementById("Comp_Type_ID").focus();
		return false;
	}
	else if(document.getElementById("Category_Type").value.trim()=="")
	{
		alertify.alert("Please fill Category Type.");
		document.getElementById("Category_Type").focus();
		return false;
	}
    else if(document.getElementById("Comp_Page_Name").value.trim()=="")
	{
		alertify.alert("Please fill Company Page Name.");
		document.getElementById("Comp_Page_Name").focus();
		return false;
	}
    else if(document.getElementById("Comp_Address").value.trim()=="")
	{
		alertify.alert("Please fill Company Address.");
		document.getElementById("Comp_Address").focus();
		return false;
	}
    else if(document.getElementById("Comp_City").value.trim()=="")
	{
		alertify.alert("Please fill Company City.");
		document.getElementById("Comp_City").focus();
		return false;
	}
    else if(document.getElementById("Comp_State").value.trim()=="")
	{
		alertify.alert("Please fill Company State.");
		document.getElementById("Comp_State").focus();
		return false;
	}
    else if(document.getElementById("Comp_Zip").value.trim()=="")
	{
		alertify.alert("Please fill Company Zip.");
		document.getElementById("Comp_Zip").focus();
		return false;
	}
    else if(document.getElementById("Comp_Zip").value.length>6)
	{
		alertify.alert("Please fill Company Zip corretly.");
		document.getElementById("Comp_Zip").focus();
		return false;
	}
    else if(document.getElementById("Comp_Phone").value.trim()=="")
	{
		alertify.alert("Please fill Company Phone.");
		document.getElementById("Comp_Phone").focus();
		return false;
	}
    else if(document.getElementById("Comp_Phone").length>10)
	{
		alertify.alert("Please fill Company Phone correctly.");
		document.getElementById("Comp_Phone").focus();
		return false;
	}
    else if(!document.getElementById("terms_check").checked)
	{
		alertify.alert("Please agree on terms and conditions.");
		document.getElementById("terms_check").focus();
		return false;
	}
	return true;
}

<?php
if(isset($msg_success) && $msg_success!="")
{
    ?>
    alertify.alert("<?php echo $msg_success; ?>");
    <?php
}

if(isset($msg_error) && $msg_error!="")
{
    ?>
    alertify.alert("<?php echo $msg_error; ?>");
    <?php
}
?>
</script>